const express = require('express');
const { user } = require('../db/config');
const router = express.Router()
const Result = require('../models/Result')
const { login, findUser } = require('../services/user')
const { md5, decoded } = require('../utils/index')
const { PWD_SALT, PRIVATE_KEY, JWT_EXPIRED } = require('../utils/constant')
const jwt = require('jsonwebtoken')

router.post('/login', function (req, res) {
    console.log(req.body);
    let { username, password } = req.body
    password = md5(`${password}${PWD_SALT}`)
    login(username, password).then(user => {
        if (!user || user.length === 0) {
            new Result("登录失败").fail(res)
        } else {
            const token = jwt.sign(
                { username },
                PRIVATE_KEY,
                { expiresIn: JWT_EXPIRED }
            )
            new Result({ token }, '登录成功').success(res)
        }
    })
})
router.get('/info', function (req, res) {
    const decode = decoded(req)
    console.log(decode);
    if (decode && decode.username) {
        findUser(decode.username).then(user => {
            console.log(user);
            if (user) {
                user.roles = [user.role]
                new Result(user, '用户信息查询成功').success(res)
            } else {
                new Result('用户信息查询失败').fail(res)
            }
        })
    } else {
        new Result('用户信息查询失败').fail(res)
    }
})
module.exports = router